package com.xiaolun.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

public class NettyByteBuf02 {
	public static void main(String[] args) {
		//创建ByteBuf
		ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!", Charset.forName("utf-8"));

		//使用相关的方法
		if (byteBuf.hasArray()) { // true
			byte[] content = byteBuf.array();

			//将 content 转成字符串
			System.out.println(new String(content, Charset.forName("utf-8")));

			System.out.println("byteBuf=" + byteBuf);
			System.out.println(byteBuf.arrayOffset()); // 0
			System.out.println(byteBuf.readerIndex()); // 0-还没进行读取
			System.out.println(byteBuf.writerIndex()); // 12-一共12个字符，从第13个开始写
			System.out.println(byteBuf.capacity()); // 36

//			System.out.println(byteBuf.getByte(0)); // 104-he对应的Acii码-不会导致byteBuf.readableBytes位置变化

			int len = byteBuf.readableBytes(); //可读的字节数  12
			System.out.println("len=" + len);

			//使用for取出各个字节
			for (int i = 0; i < len; i++) {
				System.out.println((char) byteBuf.getByte(i));
			}

			/**
			 * 按照某个范围读取.输出结果如下：
			 * hell
			 * o,worl
			 */
			System.out.println(byteBuf.getCharSequence(0, 4, Charset.forName("utf-8")));
			System.out.println(byteBuf.getCharSequence(4, 6, Charset.forName("utf-8")));
		}
	}
}
